package model.dao;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {
	Connection cn = null;
	Statement st = null;
	PreparedStatement pt = null;
	ResultSet rs = null;

	public void init() {
		try {
			cn = DriverManager.getConnection("proxool.DbPool");
			/*
			 * System.out.println("Success loading addDatabase MySQL Driver...");
			 */
		} catch (Exception e) {
			System.out.println("Error loading MySQL Driver...");
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			if (pt != null) {
				pt.close();
			}
			if (st != null) {
				st.close();
			}
			if (cn != null) {
				cn.close();
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
		}

	}

	public boolean searchUser(String User_Name) {
		int count=0;
		init();
		try {
			String sql = "select count(*) from user_info where User_Name='"
					+ User_Name + "'";
			st = cn.createStatement();
			rs = st.executeQuery(sql);
			if (rs.next()) {
				count=rs.getInt(1);
			}
			if(count==1){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			close();
		}
	}

	public void AddUser(String User_Name, String User_Pwd, String User_Sex,
			int User_Age, String User_RealName, String User_Tel,
			String User_Place, String User_Email) {
		init();
		try {
			pt = cn.prepareStatement("insert into User_Info(User_Name,User_Pwd,User_Sex,User_Age,User_RealName,User_Tel,User_Place,User_Email,User_State) values(?,?,?,?,?,?,?,?,?)");
			pt.setString(1, User_Name);
			pt.setString(2, User_Pwd);
			pt.setString(3, User_Sex);
			pt.setInt(4, User_Age);
			pt.setString(5, User_RealName);
			pt.setString(6, User_Tel);
			pt.setString(7, User_Place);
			pt.setString(8, User_Email);
			pt.setInt(9, 1);
			pt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

	public void UpdateUser(int id, String name, String pwd, String User_Sex,
			int User_Age, String User_RealName, String User_Tel,
			String User_Place, String User_Email) {
		init();
		try {
			String url = "select User_Id,User_Sex,User_Age,User_RealName,User_Tel,User_Place,User_Email from User_Info where User_Name='"
					+ name + "'and User_Pwd='" + pwd + "'";
			st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			rs = st.executeQuery(url);
			while (rs.next()) {
				rs.moveToCurrentRow();
				rs.updateString(2, User_Sex);
				rs.updateInt(3, User_Age);
				rs.updateString(4, User_RealName);
				rs.updateString(5, User_Tel);
				rs.updateString(6, User_Place);
				rs.updateString(7, User_Email);
				rs.updateRow();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

	public void UpdatePwd(String name, String oldpwd, String newpwd) {
		init();
		try {
			String url = "update User_Info set User_Pwd='" + newpwd
					+ "'where User_Name='" + name + "' and User_Pwd='" + oldpwd
					+ "'";
			st = cn.createStatement();
			st.executeUpdate(url);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

}
